const mongoose = require('mongoose');

const unreadMessageSchema = new mongoose.Schema({
    messageId: {
        type: String,
        required: true
    },
    fromUserId: {
        type: String,
        required: true
    },
    fromUsername: {
        type: String,
        required: true
    },
    type: {
        type: String,
        enum: ['group', 'private'],
        required: true
    },
    timestamp: {
        type: Date,
        default: Date.now
    }
}, { _id: false });

const userSchema = new mongoose.Schema({
    username: { 
        type: String, 
        required: true,
        unique: true 
    },
    password: {
        type: String,
        required: true
    },
    socketId: String,
    status: {
        type: String,
        enum: ['online', 'offline', 'away'],
        default: 'offline'
    },
    lastSeen: {
        type: Date,
        default: Date.now
    },
    unreadMessages: [unreadMessageSchema]
});

userSchema.methods.toJSON = function() {
    return {
        userId: this._id,
        username: this.username,
        status: this.status,
        lastSeen: this.lastSeen
    };
};

module.exports = mongoose.model('User', userSchema); 